package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.bo.PublicVideo;
import com.jrtc.base.entity.bo.PublicVideoList;
import com.jrtc.base.entity.bo.UrlClassification;
import com.jrtc.service.PublicVideoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 运动处方视频(Prescriptionvideo)表控制层
 *
 * @author makejava
 * @since 2022-09-06 15:02:35
 */
@RestController
@RequestMapping("publicVideo")
public class PublicVideoController
{
    /**
     * 服务对象
     */
    @Resource
    private PublicVideoService publicVideoService;

    /**
     * 运动处方视频列表
     * */
    @PostMapping("/urlList")
    public ResultDTO urlList(@RequestBody Map<String, Object> map)
    {
        String name = null;
        if (map.get("name") != null && !map.get("name").equals(""))
        {
            name = map.get("name").toString();
        }
        if (name == null)
        {
            name = "";
        }
        List<UrlClassification> urlClassificationList = publicVideoService.urlList(name);
        return ResultDTOBuilder.success(urlClassificationList);
    }

    /**
     * 动作库视频列表
     * @param request
     * @param publicVideo
     * @return
     */
    @PostMapping("/videoList")
    public ResultDTO videoList(HttpServletRequest request, @RequestBody PublicVideoList publicVideo) {
        String token = request.getHeader("token");
        IPage<PublicVideo> publicVideoIPage = null;
        try {
            publicVideoIPage = publicVideoService.videoList(token, publicVideo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        return ResultDTOBuilder.success(publicVideoIPage);
    }

    /**
     * 添加运动处方视频信息
     * @param request
     * @param publicVideo
     * @return
     */
    @PostMapping("/insertPrescriptionvideo")
    public ResultDTO insertPrescriptionvideo(HttpServletRequest request, @RequestBody PublicVideo publicVideo) {
        String token = request.getHeader("token");
        try {
            int prescriptionvideo = publicVideoService.insertPrescriptionvideo(token, publicVideo);
            return ResultDTOBuilder.success(prescriptionvideo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
    }

    /**
     * 修改运动处方视频信息
     * @param request
     * @param publicVideo
     * @return
     */
    @PostMapping("/updatePrescriptionvideo")
    public ResultDTO updatePrescriptionvideo(HttpServletRequest request, @RequestBody PublicVideo publicVideo) {
        String token = request.getHeader("token");
        try {
            int prescriptionvideo = publicVideoService.updatePrescriptionvideo(token, publicVideo);
            return ResultDTOBuilder.success(prescriptionvideo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
    }

    /**
     * 根据id查看运动处方视频信息详情
     * @param map
     * @return
     */
    @PostMapping("/queryPublicVideoById")
    public ResultDTO queryPublicVideoById(@RequestBody Map<String, Long> map) {
        if (map.get("prescriptionVideoId") != 0) {
            Long prescriptionVideoId = map.get("prescriptionVideoId");
            PublicVideo publicVideo = publicVideoService.queryPublicVideoById(prescriptionVideoId);
            return ResultDTOBuilder.success(publicVideo);
        } else {
            return ResultDTOBuilder.failureMsg("ID不能为0");
        }
    }
}
